package BuilderType.src;


/***
 * @Description
 * 无论是构造函数还是set方法，在复杂对象面前多个构造函数或者是单步设置属性
 * 都容易出错,，也不符合开闭原则--为了解决这两个痛点，builder模式就横空出世了。
 *
 * 在Computer 中创建一个静态内部类 Builder，然后将Computer 中的参数都复制到Builder类中。
 * 在Computer中创建一个private的构造函数，参数为Builder类型
 * 在Builder中创建一个public的构造函数，参数为Computer中必填的那些参数，cpu 和ram。
 * 在Builder中创建设置函数，对Computer中那些可选参数进行赋值，返回值为Builder类型的实例
 * 在Builder中创建一个build()方法，在其中构建Computer的实例并返回
 *
 * @Date 9:29 2022/8/8
 * @Author sunye
 **/
public class Client3 {
    public static void main(String[] args) {
        Computer3 computer=new Computer3.Builder("因特尔","三星")
                .setDisplay("三星24寸")
                .setKeyboard("罗技")
                .setUsbCount(2)
                .build();
        System.out.println(computer);
    }
}

class Computer3 {
    private final String cpu;//必须
    private final String ram;//必须
    private final int usbCount;//可选
    private final String keyboard;//可选
    private final String display;//可选

    private Computer3(Builder builder) {
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.usbCount = builder.usbCount;
        this.keyboard = builder.keyboard;
        this.display = builder.display;
    }

    public static class Builder {
        private String cpu;//必须
        private String ram;//必须
        private int usbCount;//可选
        private String keyboard;//可选
        private String display;//可选

        public Builder(String cup, String ram) {
            this.cpu = cup;
            this.ram = ram;
        }

        public Builder setUsbCount(int usbCount) {
            this.usbCount = usbCount;
            return this;
        }

        public Builder setKeyboard(String keyboard) {
            this.keyboard = keyboard;
            return this;
        }

        public Builder setDisplay(String display) {
            this.display = display;
            return this;
        }

        public Computer3 build() {
            return new Computer3(this);
        }
    }
    //省略getter方法

    @Override
    public String toString() {
        return "Computer3{" +
                "cpu='" + cpu + '\'' +
                ", ram='" + ram + '\'' +
                ", usbCount=" + usbCount +
                ", keyboard='" + keyboard + '\'' +
                ", display='" + display + '\'' +
                '}';
    }
}